import { ref, watch, reactive } from "vue";
import dateComp from "../component/dateComp.vue";
import cellTotalQuestion from "../component/cellTotalQuestion.vue";
import {Plus, Search, Delete, Upload} from "@element-plus/icons-vue";
import request from "@/utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
import $button from '@/button'

//题库类型列表
export function enableBankTypeListApi(dataRequest) {
  return request({
      url: "/course/bank/type/enable/list",
      method: "POST",
      data: dataRequest,
  });
}
// 分页
function bankListApi(dataRequest) {
  return request({
    url: "/bank/page",
    method: "POST",
    data: dataRequest,
  });
}
//
// 删除
export function courseDelApi(id) {
  return request({
    url: "/bank/"+ id,
    method: "DELETE",
  });
}
// 下架
 function bankFlagApi(dataRequest) {
  return request({
    url: "/bank/publish/flag",
    method: "PUT",
    data: dataRequest,
  });
}
// 上架
export  function bankFlagUPApi(dataRequest) {
  return request({
    url: "/bank/publish/flag",
    method: "PUT",
    data: dataRequest,
  });
}
// 题库类型
export function configBankApi(dataRequest) {
  return request({
    url: "/course/bank/type/list",
    method: "POST",
    data: dataRequest,
  });
}
// 重命名
export function nameBankApi(dataRequest) {
  return request({
    url: "/bank/name",
    method: "PUT",
    data: dataRequest,
  });
}
// 类别
export function courseAddApi(dataRequest) {
  return request({
    url: "/course/list",
    method: "POST",
    data: dataRequest,
  });
}
let tableDataInit = [];
export const operationItem = reactive({
  defaultSearch: true, //默认搜索按钮
  SearchText: "查询", //搜索文字 默认搜索
  RefreshText: "重置", //刷新文字 默认重置
  defaultRefresh: false, //默认重置按钮
  customTableHeader: true, //
  SearchItem: [
    {
      code: "name",
      name: "题库名称",
      type: "input",
    },
    {
      code: "courseId",
      name: "科目名称",
      type: "select",
      value: "id",
      label: "name",
      options: [],
    },
    {
      code: "publishFlag",
      name: "题库状态",
      type: "select",
      options: [
        {
          value: 1,
          label: "上架",
        },
        {
          value: 0,
          label: "下架",
        },
      ],
    },
    {
      code: "courseBankTypeId",
      name: "题库类型",
      type: "select",
      value: "id",
      label: "name",
      options: [],
    },
    {
      code: "daterange", //
      startName: "", //默认是开始时间 type为 daterange生效
      rangeName: "", //默认是至 type为 daterange生效
      endName: "", //默认是结束时间 type为 daterange生效
      valueFormat: "", //默认是YYYY-MM-DD
      placeholder: "请选择日期", //默认是请选择日期type为date生效
      type: "daterange", //daterange或date
    },
  ],
  //   v-hasPermi="['monitor:job:export']"
  menuArray: [
    {
      show: true,
      type: "primary",
      text: "导入",
      // hasPermi:[$button.trainbankQuestionadd||null],
      hasPermi:[$button.trainbankadd||null],
      icon: Plus,
      customParameters: "add", //自定义函数参数
      handle: () => {
        console.log("handle");
      },
    },
	  {
		  show: true,
		  type: "primary",
		  text: "导出",
		  hasPermi:[$button.trainbankadd||null],
		  icon: Upload,
		  handle: () => {
			  console.log("handle");
		  },
	  },
    {
      show: true,
      type: 'danger',
      text: '批量删除',
      disabled: true,
      // hasPermi: [$button.studentstudentremove|| null],
      icon: Delete,
      handle: () => {
      }
    },

    // /Search
  ],
});
export const tableItem = reactive({
  select: true, //多选框
  index: false, //顺序
  tree: false, //树形
  loading:false,
  expandAll: true, //树形默认展开
  align: "center",
  column: [
    {
      label: "题库编号",
      prop: "name",
      width: 300,
    },
    {
      label: "题目数量",
      // prop: "totalQuestion",
      width: 200,
      component: cellTotalQuestion,
      // defineComponent(() => {
      //     const render = () => {
      //         return <div><span>components</span></div>
      //     }
      //     return render
      // })
    },
    {
      label: "科目名称",
      prop: "courseName",
    },
    {
      label: "创建时间",
      prop: "createTime",
    },
    {
      label: "题库状态",
      // prop: "name",
      component: dateComp,
    },
    {
      label: "题库类型",
      prop: "courseBankTypeName",
    },
    {
      label: "备注",
      prop: "remark",
    },
    {
      label: "操作",
      prop: "action",
      align: "center",
      fixed: "right",
      width: "260",
    },
  ],
  pageInfo: {
    request: {
      name: "", //名称
      courseId: null, //科目id
      courseParentId: null, //类别id
      publishFlag: null, //上架标签 1-是 0-否
      courseBankTypeId: null, //题库类型id
      createTimeStart: "", //创建时间开始结点
      createTimeEnd: "", //创建时间结束结点
      pageDomain: {
        pageNum: 1, //分页页面数
        pageSize: 10, //分页页面数据大小
        orderByColumn: "", //搜索参数
        isAsc: "", //搜索参数
        reasonable: "", //搜索参数
      },
    },
    url: "",
    closeMountedPaging: true,
    paging: function () {
      if (tableItem.pageInfo.request.createDateStart) {
        tableItem.pageInfo.request.createTimeStart =
          tableItem.pageInfo.request.createDateStart + "T00:00:00";
      } else {
        tableItem.pageInfo.request.createTimeStart = null;
      }
      if (tableItem.pageInfo.request.createDateEnd) {
        tableItem.pageInfo.request.createTimeEnd =
          tableItem.pageInfo.request.createDateEnd + "T23:59:59";
      } else {
        tableItem.pageInfo.request.createTimeEnd = null;
      }
      operationItem.SearchItem[1].options = ""
      operationItem.SearchItem[3].options = ""
      tableItem.loading=true
      bankListApi(tableItem.pageInfo.request).then((res) => {
        tableItem.tableData.data = res.rows;
        tableItem.tableData.total = res.total;
        //设置搜索框的科目列表
        let parentId = tableItem.pageInfo.request.courseParentId;
        courseAddApi({levelNum: 3, parentId }).then((result) => {
          operationItem.SearchItem[1].options = result.rows;
        });
        //设置搜索框题库类型列表
        enableBankTypeListApi({courseId: parentId}).then((result) => {
          operationItem.SearchItem[3].options = result.rows;
        });
        if (!tableItem.tableData.data && tableItem.tableData.data.length == 0) {
          return;
        }
        tableItem.tableData.data.map((item) => {
          item.totalQuestion = 0;
          item.publishFlag == 1
            ? (item.closeButton = "上架删除")
            : (item.closeButton = "下架");
          // if (item.typeAmountConf) {
          //   // 题目数量
          //   for (const key in item.typeAmountConf) {
          //     if (item.typeAmountConf[key]) {
          //       item.totalQuestion += item.typeAmountConf[key];
          //     }
          //   }
          // }
        });
        tableItem.loading=false
      }).catch(()=>{tableItem.loading=false});
    },
  },
  tableData: {
    total: '',
    data: [],
  },
  btnArray: [
    {
      type: "primary",
      label: "查看",
      plain: true,
      text: true,
      hasPermi:[$button.trainbankQuestionlook||null],
      show: true,
      handle: function (row) {
        console.log("row", row);
      },
    },
    {
      label: "重命名",
      type: "primary",
      hasPermi:[$button.trainbankedit||null],
      show: true,
      text: true,
    },
    {
      label: "上架",
      type: "primary",
      show: true,
      text: true,
      hasPermi:[$button.trainbankpublish||null],
      handle: function (row) {
      },
    },
    {
      label: "下架",
      type: "danger",
      show: true,
      text: true,
      hasPermi:[$button.trainbankpublish||null],
      handle: function (row) {
        bankFlagApi({ id: row.id, publishFlag: 0 }).then((res) => {
          ElMessage.success(res.msg || "操作成功");
          tableItem.pageInfo.paging();
        });
      },
    },
    {
      label: "删除",
      hasPermi:[$button.trainbankremove||null],
      show: true,
      type: "danger",
      text: true,
      handle: function (row) {},
    },
  ],
  moreBtnArrayText: "更多", //
  moreBtnArray: [],
  selectChange: function (row) {
    console.log(row);
  },
});
